package com.ns.sbs.oauth.role.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ns.core.current.config.SysConst;
import com.ns.core.utils.StpUtil;
import com.ns.core.vo.bs.BaseVO;
import com.ns.sbs.oauth.role.dto.RolePageDto;
import com.ns.sbs.oauth.role.entry.RoleEntry;
import com.ns.sbs.oauth.role.mapper.RoleMapper;
import com.ns.sbs.oauth.role.vo.RoleEntryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色业务实现
 *
 * @author : liaowei
 * @version : v1.0.0
 * @since : 2024/8/24 22:03
 **/
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntry> implements IRoleService {
    private final RoleMapper roleMapper;

    @Override
    public RoleEntryVO getById(Long id) {
        return roleMapper.getById(id);
    }

    @Override
    public int update(RoleEntry s) {
        return roleMapper.updateById(s);
    }

    @Override
    public Page<RoleEntryVO> getList(RolePageDto dto) {
        return roleMapper.getList(dto, new Page<>(dto.getCurrent(), dto.getSize()));
    }

    @Override
    public boolean hasPermission(Long loginUserId, Long id) {
        RoleEntryVO curRole = roleMapper.getByAccId(loginUserId, StpUtil.getSid());
        RoleEntryVO oprRole = roleMapper.getByAccId(id, StpUtil.getSid());
        return curRole.getLevel() > oprRole.getLevel();
    }

    @Override
    @Cacheable(cacheNames = SysConst.ROLE_DICT, key = "'options'", unless = " #result==null ")
    public List<BaseVO> getOptions(RolePageDto dto) {
        return roleMapper.getOptions(dto);
    }
}
